package hasthi.tools.analyze;

import static hasthi.common.constants.HasthiConstants.LOGGER_NAME;
import hasthi.common.BaseContext;
import hasthi.common.HasthiException;
import hasthi.common.LogFile;
import hasthi.common.Utils;
import hasthi.common.HasthiException.FaultCode;
import hasthi.container.ManagmentNode;
import hasthi.instrument.HostAgent;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

/**
 * This Managers dies automatically in one hour, used for performance tests
 */
public class TestManager extends hasthi.container.ManagmentNode{
    public static long TEST_ROUND_TIME = 1*60*60*1000;
    private static Log log = LogFactory.getLog(LOGGER_NAME);
    
    public static void main(String[] args) throws HasthiException {
        try {
            Properties properties = null;
            
            if(args.length > 0){
                properties =  new Properties();
                FileInputStream propertyStream = new FileInputStream(args[0]);
                properties.load(propertyStream);
                propertyStream.close();
            }
            Thread t = new Thread(new Runnable() {
                public void run() {
                    try {
                        Thread.sleep(TEST_ROUND_TIME);
                        System.out.println("Shutting down Manager at "+ Utils.getLocalHost());
                        try {
                            LogFile.flushAll();
                            LogManager.shutdown();
                        } catch (RuntimeException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }                        
                        System.exit(0);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            
            });
            t.start();
            
            BaseContext baseContext = new BaseContext(properties);
            
            //HostAgent.main(args);
            Thread.sleep(Utils.randomInt(10000));
            int retry = 0;
            while(retry < 3){
                try {
                    ManagmentNode node = new ManagmentNode(baseContext);
                    node.startManagmentNode();
                    return;
                } catch (Exception e) {
                    e.printStackTrace();
                    retry++;
                    log.warn("Retry managment node "+ retry+ "");
                    Thread.sleep(Utils.randomInt(30000));
                }
            }
            
        } catch (FileNotFoundException e) {
            throw new HasthiException(e,FaultCode.LocalError);
        } catch (IOException e) {
            throw new HasthiException(e,FaultCode.LocalError);
        } catch (InterruptedException e) {
            throw new HasthiException(e,FaultCode.LocalError);
        }
    }

    public TestManager(Properties properties) throws HasthiException {
        super(properties);
    }
}
